<?php
    require('config/config.php');
    include('header.php');
    $machines = Machine::rechercheTout();
    
    if(\Utils\Outils::getValue("envoie")) {
        $machine = Machine::recherche(intval(\Utils\Outils::getValue("id")));
        $machine->reinitialisationRestreindre();
        foreach(\Utils\Outils::getValue("typeEvenements") as $te) {
            $typeEvenement = TypeEvenement::recherche(intval($te));
            $restreindre = new \Restreindre(0, $typeEvenement, $machine);
            $restreindre->ajout();
        }
    }
?>
       <!-- Header -->
    <header class="container_12 " id="header">
        <div class="grid_3 alpha">
            <img src="./stylesheets/images/logo1.png" alt="Logo"/>
        </div>
<?php
    require 'tableauCriticite.php';
?>
    </header>
    <!-- End Header -->
    <!-- Menu -->
    <div class="clear"></div>
    <nav class="content">
<?php
    require 'menu.php';
?>
    <!-- End Menu -->
    
    <!-- Content -->
    <form class="compte" method="POST" action="">
            <fieldset>
                <legend>Ajouter un nouvel événement</legend><br>
 
                    <p><label class="grid_4" for="machine">Machine :</label></p>
                    <select class="grid_3" name="machine" id="machine" onChange="ajoutEvenement()">
<?php
    foreach($machines as $machine) {
        echo             '<option value="'.$machine->getId().'">'.$machine.'</option>';
    }
?>
                    </select>
                    <div class="clear"></div>
<?php
    foreach($machines as $index => $machine) {
        $restreindres = $machine->getRestreindres();
        $type_evenements = $machine->getEvenementsRestants();
        if($index !== 0) {
            $style = 'display:none;';
        }
        else {
            $style = '';
        }
        echo        '<div id="machine_id'.$machine->getId().'" class="divMachine" style="'.$style.'">
                        <p>
                            <label class="grid_4" for="machine_ip">IP :</label>
                            <input class="grid_3" type="text" name="ip" id="machine_ip" readonly="readonly" value="'.$machine->getAdresseIp().'">
                        </p>
                        <div class="clear"></div>
                        <p><label class="grid_4" for="event">Evénements à ajouter :</label></p>
                        <form method="POST" action="ajoutEvenement.php">
                            <input type="hidden" name="id" value="'.$machine->getId().'" />
                            <select class="grid_3" name="typeEvenements[]" multiple style="height: 80px;width: 200px;">';
        foreach($restreindres as $restreindre) {
            echo                '<option value="'.$restreindre->getTypeEvenement()->getId().'" selected>'.$restreindre->getTypeEvenement().'</option>';
        }
        foreach($type_evenements as $type_evenement) {
            echo                '<option value="'.$type_evenement->getId().'">'.$type_evenement.'</option>';
        }
        echo                '</select>
                            <input type="submit" name="envoie" value="Modifier" />
                        </form>
                    </div>';
    }
?>
                </fieldset>
        </form>
    </nav>
    
    
    <!-- End content-->
    
    <!-- Footer -->
    <div class="clear"></div>
<?php
    include 'footer.php';
 ?>
    <!-- End Footer -->
